package com.yuan.fullstackback.controller;

import com.yuan.fullstackback.pojo.SysArticle;
import com.yuan.fullstackback.service.impl.SysArticleServiceImpl;
import com.yuan.fullstackback.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/article")
public class SysArticleController {

    @Autowired
    private SysArticleServiceImpl sysArticleService;

    /**
     * 分页查询所有的文章
     *
     * @return 文章数据
     */
    @GetMapping("/list")
    public AjaxResult listArticle() {
        List<SysArticle> articles = sysArticleService.selectAllArticlePage();
        return AjaxResult.success(articles);
    }

    /**
     * 添加文章
     */
    @PostMapping("/add")
    public AjaxResult addArticle(@RequestBody @Validated SysArticle sysArticle) {
        if (sysArticleService.addArticle(sysArticle) != 1)
            return AjaxResult.error("新增文章'" + sysArticle.getTitle() + "'失败");
        return AjaxResult.success("新增成功");
    }

    /**
     * 修改文章
     */
    @PostMapping("/update")
    public AjaxResult updateArticle(@RequestBody @Validated SysArticle sysArticle) {
        if (sysArticleService.updateArticle(sysArticle) != 1)
            return AjaxResult.error("修改文章'" + sysArticle.getTitle() + "'失败");
        return AjaxResult.success("修改成功");
    }

    /**
     * 删除文章[逻辑删除]
     */
    @PostMapping("/delete")
    public AjaxResult deleteArticle(@RequestBody @Validated Long articleId) {
        if (sysArticleService.deleteArticle(articleId) != 1)
            return AjaxResult.error("删除文章'" + articleId + "'失败");
        return AjaxResult.success("删除成功");
    }

}
